/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-02-13
 * Time: 11:28
 */
class Solution14 {
    public int[][] generateMatrix(int n) {
        int row = n;
        int column = row;
        int[][] ret = new int[row][row];
        int l = 0, r = column - 1, u = 0, d = row - 1, count = 1;
        while (true) {
            for (int i = l; i <= r; i++) { //行不变列变 u是行l是列，u行不能超过d
                ret[u][i] = count;
                count++;
            }
            if (++u > d) break;
            for (int i = u; i <= d; i++) {//列不变行变，r是列u是行，r列不能小于l
                ret[i][r] = count;
                count++;
            }
            if (--r < l) break;
            for (int i = r; i >= l; i--) {
                ret[d][i] = count;// 行不变列变，d是行r是列，d行不能小于u
                count++;
            }
            if (--d < u) break;
            for (int i = d; i >= u; i--) {// 列不变行变，以l为列，d为行,l不能大于r
                ret[i][l] = count;
                count++;
            }
            if (++l > r) break;
        }

        return ret;

    }
}
public class Test14 {

}
